﻿#include "GUISEASubSysBaseDialog.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractSEASubSys.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h" 
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKCore/FITKActionOperator.h"

#include "PickDataProvider/PickedDataProvider.h"
#include "PickDataProvider/PickedData.h"
#include "OperatorsInterface/GraphEventOperator.h"

#include <QMessageBox>

namespace GUI {
    GUISEASubSysBaseDialog::GUISEASubSysBaseDialog(Acoustics::FITKAbstractSEASubSys* obj, Core::FITKActionOperator* oper, QWidget* parent /*= nullptr*/) :
        GUIDialogBase(parent),
        _obj(obj),
        _oper(oper)
    {
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        Qt::WindowFlags flags = windowFlags();
        flags &= ~Qt::WindowContextHelpButtonHint;
        setWindowFlags(flags);
        //获取是否创建数据
        _oper->argValue<bool>("IsCreate", _isCreate);
    }

    GUISEASubSysBaseDialog::~GUISEASubSysBaseDialog()
    {
        //销毁对象
    }

    void GUISEASubSysBaseDialog::initData()
    {
        //初始化，需要在基类重写
    }

    void GUISEASubSysBaseDialog::reject()
    {
        //清除拾取模式和高亮
        GUI::GUIPickInfo::ClearPickInfo();
        //关闭窗口
        _oper->setArgs("DataID", -1);
        if (_isCreate)
        {
            delete _obj;
            _obj = nullptr;
        }
        disHight();
        this->QDialog::reject();
    }

    void GUISEASubSysBaseDialog::accept()
    {
        disHight();
        GUIDialogBase::accept();
    }

    bool GUISEASubSysBaseDialog::createObjData()
    {
        if (!_oper || !_obj) return false;
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return false;
        //获取SEA点数据
        Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        if (!seaModel)return false;
        Acoustics::FITKAcousticsSEASubSysManager* subSysMgr = seaModel->getSEASubSysManager();
        if (!subSysMgr) return false;

        //获取板名称-查重处理
        QString name = this->getNameFormWidget();
        if (subSysMgr->getDataByName(name)) {
            QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
            return false;
        }
        //设置名称
        _obj->setDataObjectName(name);
        _pyStr.append(QString("seaSubSys.setName('%1')").arg(name));
        //设置界面数据
        this->getDataFormWidget();
        _oper->setArgs("objID", _obj->getDataObjectID());
        //清除拾取模式和高亮
        GUI::GUIPickInfo::ClearPickInfo();
        return true;
    }

    bool GUISEASubSysBaseDialog::editObjData()
    {
        if (!_oper || !_obj) return false;
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return false;
        //获取SEA点数据
        Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        if (!seaModel)return false;
        Acoustics::FITKAcousticsSEASubSysManager* subSysMgr = seaModel->getSEASubSysManager();
        if (!subSysMgr) return false;
        //获取板名称
        QString name = this->getNameFormWidget();
        //名称重复判断
        if (name != _obj->getDataObjectName())
            if (subSysMgr->getDataByName(name)) {
                QMessageBox::warning(this, "", tr("\"%1\" already exists and cannot be overwritten.").arg(name), QMessageBox::Ok);
                return false;
            }
        _obj->setDataObjectName(name);
        _pyStr.append(QString("seaSubSys.setName('%1')").arg(name));

        this->getDataFormWidget();
        _oper->setArgs("objID", _obj->getDataObjectID());

        return true;
    }

    QString GUISEASubSysBaseDialog::ckeckNameFromMgr(QString name)
    {
        //获取离散数据
        Acoustics::FITKAcousticsDiscModel* discModel = FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (!discModel)return name;
        //获取SEA点数据
        Acoustics::FITKAcousticsSEAModel* seaModel = discModel->getSEAModel();
        if (!seaModel)return name;
        Acoustics::FITKAcousticsSEASubSysManager* subSysMgr = seaModel->getSEASubSysManager();
        if (!subSysMgr) return name;
        return subSysMgr->checkName(name);
    }

    void GUISEASubSysBaseDialog::getDataFormWidget()
    {
        //从界面获取数据-基类实现
    }

    QString GUISEASubSysBaseDialog::getNameFormWidget()
    {
        //从界面里获取设置的名称-基类实现
        return QString();
    }

    QString GUISEASubSysBaseDialog::getIDListConsolidatedStr(QList<int> idList)
    {
        //从小到大排序
        QList<int> memberList = idList;
        qSort(memberList);
        if (memberList.isEmpty()) return QString();
        else if (memberList.size() == 1) return QString::number(memberList.at(0));
        else if (memberList.size() == 2) return QString("%1, %2").arg(memberList.at(0)).arg(memberList.at(1));
        //获取排序后的结果
        QString results = QString::number(memberList.at(0));
        for (int i = 1; i < memberList.size() - 1; ++i)
        {
            int lastID = memberList.at(i - 1);
            int currentID = memberList.at(i);
            int nextID = memberList.at(i + 1);
            if (results.back() == ' ')
                results += QString("%1").arg(currentID);
            if (lastID + 1 == currentID && results.back() != '-')
                results += "-";
            else if (nextID - 1 != currentID)
                results += QString("%1, ").arg(currentID);
        }
        results += QString::number(memberList.back());
        return results;
    }

    void GUISEASubSysBaseDialog::showEvent(QShowEvent * event)
    {
        if (_obj && _isCreate == false) {
            GUIDialogBase::hightGraphObj(_obj->getDataObjectID());
        }
        GUIDialogBase::showEvent(event);
    }

    void GUISEASubSysBaseDialog::disHight()
    {
        if (_obj && _isCreate == false) {
            GUIDialogBase::disHightGraphObj(_obj->getDataObjectID());
        }
    }
}
